package com.zbkj.service.service.mszs;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.mszs.MszsRoomType;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.mszs.MszsRoomTypeRequest;
import com.zbkj.common.request.mszs.MszsRoomTypeSelectRequest;
import com.zbkj.common.response.mszs.MszsRoomTypeResponse;
import com.zbkj.service.dao.mszs.MszsRoomTypeDao;
import com.zbkj.service.service.publics.attachment.SystemAttachmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @description 民宿助手房型信息
 * @author zhengkai.blog.csdn.net
 * @date 2023-11-16
 */
@Service
public class MszsRoomTypeServiceImpl extends ServiceImpl<MszsRoomTypeDao, MszsRoomType> implements MszsRoomTypeService {

    @Autowired
    private MszsRoomTypeDao roomTypeDao;
    @Autowired
    private SystemAttachmentService systemAttachmentService;
    @Override
    public boolean insert(MszsRoomTypeRequest request) {

        MszsRoomType roomType = new MszsRoomType();
        BeanUtils.copyProperties(request, roomType);
        if (request.getOutdoor() != null && request.getOutdoor().size()>0){//判断是否有图片
            //list转tring
            String outImage = StringUtils.join(request.getOutdoor(),",");
            roomType.setOutdoor(systemAttachmentService.clearPrefix(outImage));
        }
        if (request.getFjImage() != null && request.getFjImage().size()>0){//判断是否有图片
            //list转tring
            String image = StringUtils.join(request.getFjImage(),",");
            roomType.setFjImage(systemAttachmentService.clearPrefix(image));
        }

        int ro = roomTypeDao.insert(roomType);
        return ro > 0;

    }

    @Override
    public boolean update(MszsRoomTypeRequest request) {
        MszsRoomType roomType = new MszsRoomType();
        BeanUtils.copyProperties(request, roomType);
        if (request.getOutdoor() != null && request.getOutdoor().size()>0){ //判断是否有图片
            //list转string
            String outImage = StringUtils.join(request.getOutdoor(),",");
            roomType.setOutdoor(systemAttachmentService.clearPrefix(outImage));
        }
        if (request.getFjImage()!= null && request.getFjImage().size()>0){ //判断是否有图
            //list转string
            String image = StringUtils.join(request.getFjImage(),",");
            roomType.setFjImage(systemAttachmentService.clearPrefix(image));
        }
        int ro = roomTypeDao.updateById(roomType);
        return ro > 0;
    }

    @Override
    public boolean delete(Integer id) {
        int ro = roomTypeDao.deleteById(id);
        return ro > 0;
    }

    @Override
    public MszsRoomTypeResponse load(Integer id) {
        MszsRoomType roomType = roomTypeDao.selectById(id);
        if (roomType != null){
            MszsRoomTypeResponse roomResponse=new MszsRoomTypeResponse();
            BeanUtils.copyProperties(roomType,roomResponse);
            if (StringUtils.isNotEmpty(roomType.getFjImage())){ //判断是否有图片
                //string转list
                List<String> imgs= Arrays.asList(roomType.getFjImage().split(","));
                roomResponse.setFjImage(imgs);
            }
            if (StringUtils.isNotEmpty(roomType.getOutdoor())){ //判断是否有图片
                //string转list
                List<String> outdoor= Arrays.asList(roomType.getOutdoor().split(","));
                roomResponse.setOutdoor(outdoor);
            }

            return roomResponse;
        }
        return null;
    }

    @Override
    public List<MszsRoomType> pageList(MszsRoomTypeSelectRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<MszsRoomType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getFxName())){
            lambdaQueryWrapper.like(MszsRoomType::getFxName, request.getFxName());
        }
        lambdaQueryWrapper.orderByDesc(MszsRoomType::getCreateTime);

        List<MszsRoomType> hotelList = roomTypeDao.selectList(lambdaQueryWrapper);
        if (CollUtil.isEmpty(hotelList)) {
            return CollUtil.newArrayList();
        }
        return hotelList;
    }

}
